from openpyxl import load_workbook
from openpyxl import Workbook
import os
import random
ok_path = 'd:/ok_data/'
local_path = 'd:/data/'
new_path = 'd:/new_data/'
for root, dirs, files in os.walk(ok_path):
    for file in files:
        file_path = os.path.join(root, file)
        if(file[:3] != "CNC" and file[:3] != "APP" and file[-2:] != "db" and file[-3:] != "tmp" and file[-3:] != "xls"):
            wb=load_workbook(file_path)#打开文件
            ws=wb.active
            max_row=ws.max_row#获取最大行数
            print(max_row)
            for root1, dirs1, files1 in os.walk(local_path):
                for file1 in files1:
                    file_path1 = os.path.join(root1, file1)
                    if(file == file1):#如果文件名相同
                        print("找到匹配文件")
                        wb1=load_workbook(file_path1)#打开文件
                        ws1=wb1.active
                        max_row1=ws1.max_row
                        if(max_row < max_row1):
                            for ii in range(max_row+1,max_row1+1):
                                local=[]
                                for iii in range(1,ws1.max_column+1):
                                    if iii>3:
                                        if ws1.cell(ii,iii).value<=ws1.cell(3,iii).value:
                                            ws1.cell(ii,iii).value=round(ws1.cell(3,iii).value+random.uniform(0.002,0.008),3)
                                        if ws1.cell(ii,iii).value>=ws1.cell(2,iii).value:
                                            ws1.cell(ii,iii).value=round(ws1.cell(2,iii).value-random.uniform(0.002,0.008),3)
                                    local.append(ws1.cell(ii,iii).value)
                                ws.append(local)
            wb.save(new_path+file)
            print("单个文件保存成功")
print("程序已执行完成")